﻿#include"XQAccountData.h"
#include<QBuffer>
XQAccountData::XQAccountData(bool base64, const QVariantMap& datas)
{
	setDatas(datas, base64);
}
XQAccountData::XQAccountData(const QStringList& datas)
{
	setDatas(datas);
}
XQAccountData::XQAccountData(bool base64, const QVariantList& datas)
{
	setDatas(datas, base64);
}
QStringList XQAccountData::Fields()
{
	return { "ctime", "mtime","portrait_data","portrait_format","account",
		"password","email","phone","name","gender" ,"permission" };
}
QStringList XQAccountData::fields() const
{
	return XQAccountData::Fields();
}
QVariantMap XQAccountData::toVariantMap(bool base64,const QStringList& fields) const
{
	QStringList f=fields;
	QVariantMap map;
	if (f.isEmpty())
		f = this->fields();
	if (f.contains("ctime"))
		map["ctime"] = ctime;
	if (f.contains("mtime"))
		map["mtime"] = mtime;
	if (f.contains("portrait_data"))
	{
		// 将QImage对象转换为字节数组
		QByteArray bytes;
		QBuffer buffer(&bytes);
		buffer.open(QIODevice::WriteOnly);
		portrait_data.save(&buffer, portrait_format.toUtf8().data());
		buffer.close();
		if(base64)
			map["portrait_data"] = bytes.toBase64();
		else
			map["portrait_data"] = bytes;
	}
	if (f.contains("portrait_format"))
		map["portrait_format"] = portrait_format;
	if (f.contains("account"))
		map["account"] = account;
	if (f.contains("password"))
		map["password"] = password;
	if (f.contains("email"))
		map["email"] = email;
	if (f.contains("phone"))
		map["phone"] = phone;
	if (f.contains("name"))
		map["name"] = name;
	if (f.contains("gender"))
		map["gender"] = int(gender);
	if (f.contains("permission"))
		map["permission"] = int(permission);
	return map;
	/*return {
		{"ctime",ctime},
		{"mtime",mtime},
		{"portrait_data",bytes},
		{"portrait_format",portrait_format},
		{"account",account},
		{"password",password},
		{"email",email},
		{"phone",phone},
		{"name",name},
		{"gender",int(gender)},
		{"permission",int(permission)}
	};*/
}

void XQAccountData::setDatas(const QVariantMap& datas, bool base64)
{
	if(datas.contains("portrait_format"))
		portrait_format = datas["portrait_format"].toString();
	if (datas.contains("portrait_data"))
	{
		if(base64)
			portrait_data = QImage::fromData(QByteArray::fromBase64(datas["portrait_data"].toByteArray()), portrait_format.toUtf8().data());
		else
			portrait_data = QImage::fromData(datas["portrait_data"].toByteArray(), portrait_format.toUtf8().data());
	}
	if (datas.contains("ctime"))
		ctime = datas["ctime"].toDateTime();
	if (datas.contains("mtime"))
		mtime = datas["mtime"].toDateTime();
	if (datas.contains("account"))
		account = datas["account"].toString();
	if (datas.contains("password"))
		password = datas["password"].toString();
	if (datas.contains("phone"))
		phone= datas["phone"].toString();
	if (datas.contains("email"))
		email = datas["email"].toString();
	if (datas.contains("name"))
		name = datas["name"].toString();
	if (datas.contains("gender"))
		gender = GenderType(datas["gender"].toInt());
	if (datas.contains("permission"))
		permission = Permission(datas["permission"].toInt());
}

void XQAccountData::setDatas(const QStringList& datas, const QStringList& fields)
{
	XQDatabaseData::setDatas(datas,fields);
}

void XQAccountData::setDatas(const QVariantList& datas, bool base64, const QStringList& fields)
{
	XQDatabaseData::setDatas(datas, base64,fields);
}

QString XQAccountData::createTableSql(const QString& tableName)
{
	return QString( "CREATE TABLE `%1` ("
		"account VARCHAR(50) PRIMARY KEY,"//账号
		"password VARCHAR(50),"//密码
		"name VARCHAR(20),"//名字
		"email VARCHAR(50),"//邮箱
		"phone VARCHAR(16),"//手机号
		"portrait_data MediumBlob,"//头像数据
		"portrait_format VARCHAR(10),"//头像格式
		"ctime DATETIME ,"//创建时间
		"mtime DATETIME ,"//修改时间
		"gender INT ,"//性别
		"permission INT"//权限
		")").arg(tableName);
}


